import { createRouter, createWebHashHistory } from 'vue-router';
import '../utils/login';
import store from '../store/index'
import { isAuthenticated } from '../utils/login';
import config from '../config/myConfig';
import LoginBox from '../view/Login/LoginBox.vue';
import Login from '../view/Login/Login.vue';
import Registered from '../view/Login/Registered.vue';
import Admin from '../view/Admin/Admin.vue';
import Submit from '../view/Submit/Submit.vue';
import jobDetail from '../view/Submit/jobDetail.vue';
const logoChildren = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/register',
    name: 'Registered',
    component: Registered,
  },
];
const routes = [
  {
    path: '/login',
    name: 'LoginBox',
    component: LoginBox,
    redirect: logoChildren[0].path, // 设置默认登录页面
    meta: {
      title: '登录',
    },
    children: logoChildren,
  },
  {
    path: '/',
    name: 'Submit',
    component: Submit,
    meta: {
      title: '主页',
       requiresAuth :true
    },
    children: [],
  },
  {
    path: '/admin',
    name: 'Admin',
    component: Admin,
    meta: {
      title: '管理员',
       requiresAuth :true
    },
    children: [],
  },
  {
    path: '/jobDetail',
    name: 'jobDetail',
    component: jobDetail,
    meta: {
      title: '作业详情',
      requiresAuth :true
    },
    children: [],
  },
];

const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHashHistory(),
  routes,
});
// 全局路由守卫
router.beforeEach(async (to) => {
  // 修改不同路由的页面标题
  const hasTitle = to?.meta?.title;
  document.title = hasTitle ? `云骄云骥作业提交系统_${hasTitle }`: '云骄云骥作业提交系统';
  if (!config.useBeforeEach) {
    return;
  }
//   if (
//     // 该路由是否只有登录用户才能访问
//     to.meta.requiresAuth &&
//     //  避免无限重定向
//     to.name !== 'Login'
//   ) {

//     // 将用户重定向到登录页面
//     // return { name: 'Login' };
//   }
});
export default router;
